package com.skt.common_hr.yearadjustment.view
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.events.DonationItemEvent;
	import com.skt.common_hr.yearadjustment.model.BizNumValidProxy;
	import com.skt.common_hr.yearadjustment.model.CreditCard2Proxy;
	import com.skt.common_hr.yearadjustment.model.CreditInhabiComboProxy;
	import com.skt.common_hr.yearadjustment.view.components.popCreditCard2;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	public class popCreditCard2Mediator extends Mediator
	{
		public static const NAME:String = "popCreditCar2dMediator"; 
		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;
		
		//추가, 수정 구분
		private var bAdd:Boolean = true;	//추가 모드
		//수정시 선택된 아이템 인덱스
		private var itemIdx:int = -1;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function popCreditCard2Mediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		public function initView():void
 		{
 			view.addEventListener(popCreditCard2.OkInput, onOkInput);
 			view.addEventListener(popCreditCard2.CancelInput, onCancelInput);
 			view._isEnable = v_enable;
 			
 			gubunComboSelect();
 			comboJumin();
 			//creditCard2ListSelect();
 		}
 		
 		//주민등록번호 콤보
 		private function comboJumin():void{
 			var proxy:CreditInhabiComboProxy = facade.retrieveProxy(CreditInhabiComboProxy.NAME) as CreditInhabiComboProxy;
 			proxy.comboload(Variables.yearPernr, v_year);
 			
 		}
 		
 		//비용유형 콤보박스 로드
 		private function gubunComboSelect():void{
 			var proxy:CreditCard2Proxy = facade.retrieveProxy(CreditCard2Proxy.NAME) as CreditCard2Proxy;
 			proxy.comboload();
 		}
 		
 		//리스트 내용 로드
 		private function creditCard2ListSelect():void{
 			var proxy:CreditCard2Proxy = facade.retrieveProxy(CreditCard2Proxy.NAME) as CreditCard2Proxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		//저장
 		private function onOkInput(event:Event):void{
 			
 			var detailList:Array = new Array();
	 		for each(var rec:Object in view.creditCard2Data)
	 		{
	 			detailList.push(rec);
	 		}
 			
 			var proxy:CreditCard2Proxy = facade.retrieveProxy(CreditCard2Proxy.NAME) as CreditCard2Proxy;
 			proxy.save(Variables.yearPernr, v_year, detailList);
 		}
 		
 		
 		//팝업 닫기
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popCreditCard2Mediator.NAME);
 			PopUpManager.removePopUp(view);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					YearAdjustmentFacade.LOAD_COMBO_GUBUN_COMPLETE,
					YearAdjustmentFacade.LOAD_COMBO_GUBUN_FAILED,
					YearAdjustmentFacade.LOAD_COMBO_CREDITINHAB_COMPLETE,
					YearAdjustmentFacade.LOAD_COMBO_CREDITINHAB_FAILED,
					YearAdjustmentFacade.LOAD_CREDITCARD2_LIST_COMPLETE,
					YearAdjustmentFacade.LOAD_CREDITCARD2_LIST_FAILED,
					YearAdjustmentFacade.SAVE_CREDITCARD2_LIST_COMPLETE,
					YearAdjustmentFacade.SAVE_CREDITCARD2_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			var control:String = note.getName();
			trace("[ @ popCreditCard2Mediator noti message ] = " + control);
			switch(control) 
			{
				case YearAdjustmentFacade.LOAD_COMBO_GUBUN_COMPLETE:
					creditCard2ListSelect();
					view.comboGubun = (note.getBody() as CreditCard2Proxy).comboGubun;
					view.comboPeriod = (note.getBody() as CreditCard2Proxy).comboPeriod;
					break;
				case YearAdjustmentFacade.LOAD_COMBO_GUBUN_FAILED:
					break;
				case YearAdjustmentFacade.LOAD_COMBO_CREDITINHAB_COMPLETE:
					view.comboInhabitants = (note.getBody() as CreditInhabiComboProxy).comboInhabitants;
					break;
				case YearAdjustmentFacade.LOAD_COMBO_CREDITINHAB_FAILED:
					break;
				
				case YearAdjustmentFacade.LOAD_CREDITCARD2_LIST_COMPLETE:
					view.creditCard2Data = (note.getBody() as CreditCard2Proxy).creditCard2Data;
					break;
				case YearAdjustmentFacade.LOAD_CREDITCARD2_LIST_FAILED:
					break;		
				
				case YearAdjustmentFacade.SAVE_CREDITCARD2_LIST_COMPLETE:
					onCancelInput(null);
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					break;
				case YearAdjustmentFacade.SAVE_CREDITCARD2_LIST_FAILED:
					PsHrAlert.showMessage((note.getBody() as CreditCard2Proxy).msgStr);
					break;	
						
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():popCreditCard2
        {
            return viewComponent as popCreditCard2;
        }
	}
}